package cn.com.lyj6851.modules.wechat;

import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.mp.domain.YdyWechatLiveGoods;
import cn.com.lyj6851.modules.mp.service.YdyWechatLiveGoodsService;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatLiveGoodsDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatLiveGoodsQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@AllArgsConstructor
@Api(tags = "ydyWechatLiveGoods管理")
@RestController
@RequestMapping("/api/ydyWechatLiveGoods")
public class YdyWechatLiveGoodsController {

    private final YdyWechatLiveGoodsService ydyWechatLiveGoodsService;
    private final IGenerator generator;

    @AopLog("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@per.check('admin','WechatLiveGoods:list')")
    public void download(HttpServletResponse response, YdyWechatLiveGoodsQueryCriteria criteria) throws IOException {
        ydyWechatLiveGoodsService.download(generator.convert(ydyWechatLiveGoodsService.queryAll(criteria), YdyWechatLiveGoodsDto.class), response);
    }

    @GetMapping
    @AopLog("查询ydyWechatLiveGoods")
    @ApiOperation("查询ydyWechatLiveGoods")
    @PreAuthorize("@per.check('admin','WechatLiveGoods:list')")
    public ResponseEntity<Object> getYdyWechatLiveGoodss(YdyWechatLiveGoodsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ydyWechatLiveGoodsService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @AopLog("新增ydyWechatLiveGoods")
    @ApiOperation("新增ydyWechatLiveGoods")
    @PreAuthorize("@per.check('admin','WechatLiveGoods:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YdyWechatLiveGoods resources){
        return new ResponseEntity<>(ydyWechatLiveGoodsService.saveGoods(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @AopLog("修改ydyWechatLiveGoods")
    @ApiOperation("修改ydyWechatLiveGoods")
    @PreAuthorize("@per.check('admin','WechatLiveGoods:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YdyWechatLiveGoods resources){
        ydyWechatLiveGoodsService.updateGoods(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @AopLog("删除ydyWechatLiveGoods")
    @ApiOperation("删除ydyWechatLiveGoods")
    @PreAuthorize("@per.check('admin','WechatLiveGoods:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
                ydyWechatLiveGoodsService.removeGoods(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("同步数据")
    @PostMapping("/synchro")
    public ResponseEntity<Object> synchroWxOlLiveGoods(@RequestBody Integer[] ids) {
        ydyWechatLiveGoodsService.synchroWxOlLive(Arrays.asList(ids));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
